package com.xbao.dao;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.xbao.model.StuContract;
import com.xbao.model.StuContractExample.Criteria;
import com.xbao.model.StuContractExample.Criterion;
import com.xbao.model.StuContractExample;
import java.util.List;
import java.util.Map;

public class StuContractSqlProvider {

    public String countByExample(StuContractExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("stu_contract");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(StuContractExample example) {
        BEGIN();
        DELETE_FROM("stu_contract");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(StuContract record) {
        BEGIN();
        INSERT_INTO("stu_contract");
        
        if (record.getSid() != null) {
            VALUES("sid", "#{sid,jdbcType=BIGINT}");
        }
        
        if (record.getCreatetime() != null) {
            VALUES("createtime", "#{createtime,jdbcType=BIGINT}");
        }
        
        if (record.getBegintime() != null) {
            VALUES("begintime", "#{begintime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            VALUES("endtime", "#{endtime,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            VALUES("subid", "#{subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotalrecords() != null) {
            VALUES("totalrecords", "#{totalrecords,jdbcType=INTEGER}");
        }
        
        if (record.getChargetime() != null) {
            VALUES("chargetime", "#{chargetime,jdbcType=BIGINT}");
        }
        
        if (record.getType() != null) {
            VALUES("type", "#{type,jdbcType=INTEGER}");
        }
        
        if (record.getPayable() != null) {
            VALUES("payable", "#{payable,jdbcType=INTEGER}");
        }
        
        if (record.getPayreal() != null) {
            VALUES("payreal", "#{payreal,jdbcType=INTEGER}");
        }
        
        if (record.getDiscountrate() != null) {
            VALUES("discountrate", "#{discountrate,jdbcType=INTEGER}");
        }
        
        if (record.getDiscount() != null) {
            VALUES("discount", "#{discount,jdbcType=INTEGER}");
        }
        
        if (record.getOthercharge() != null) {
            VALUES("othercharge", "#{othercharge,jdbcType=INTEGER}");
        }
        
        if (record.getComments() != null) {
            VALUES("comments", "#{comments,jdbcType=VARCHAR}");
        }
        
        if (record.getOperatorid() != null) {
            VALUES("operatorid", "#{operatorid,jdbcType=BIGINT}");
        }
        
        if (record.getPaytype() != null) {
            VALUES("paytype", "#{paytype,jdbcType=INTEGER}");
        }
        
        if (record.getOrgid() != null) {
            VALUES("orgid", "#{orgid,jdbcType=BIGINT}");
        }
        
        if (record.getDiscounttype() != null) {
            VALUES("discounttype", "#{discounttype,jdbcType=INTEGER}");
        }
        
        if (record.getSinglecharge() != null) {
            VALUES("singlecharge", "#{singlecharge,jdbcType=INTEGER}");
        }
        
        if (record.getClasscharge() != null) {
            VALUES("classcharge", "#{classcharge,jdbcType=INTEGER}");
        }
        
        if (record.getOnrecord() != null) {
            VALUES("onrecord", "#{onrecord,jdbcType=INTEGER}");
        }
        
        if (record.getGiverecords() != null) {
            VALUES("giverecords", "#{giverecords,jdbcType=INTEGER}");
        }
        
        return SQL();
    }

    public String selectByExample(StuContractExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("sid");
        SELECT("createtime");
        SELECT("begintime");
        SELECT("endtime");
        SELECT("subid");
        SELECT("totalrecords");
        SELECT("chargetime");
        SELECT("type");
        SELECT("payable");
        SELECT("payreal");
        SELECT("discountrate");
        SELECT("discount");
        SELECT("othercharge");
        SELECT("comments");
        SELECT("operatorid");
        SELECT("paytype");
        SELECT("orgid");
        SELECT("discounttype");
        SELECT("singlecharge");
        SELECT("classcharge");
        SELECT("onrecord");
        SELECT("giverecords");
        FROM("stu_contract");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        StuContract record = (StuContract) parameter.get("record");
        StuContractExample example = (StuContractExample) parameter.get("example");
        
        BEGIN();
        UPDATE("stu_contract");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getSid() != null) {
            SET("sid = #{record.sid,jdbcType=BIGINT}");
        }
        
        if (record.getCreatetime() != null) {
            SET("createtime = #{record.createtime,jdbcType=BIGINT}");
        }
        
        if (record.getBegintime() != null) {
            SET("begintime = #{record.begintime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            SET("endtime = #{record.endtime,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            SET("subid = #{record.subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotalrecords() != null) {
            SET("totalrecords = #{record.totalrecords,jdbcType=INTEGER}");
        }
        
        if (record.getChargetime() != null) {
            SET("chargetime = #{record.chargetime,jdbcType=BIGINT}");
        }
        
        if (record.getType() != null) {
            SET("type = #{record.type,jdbcType=INTEGER}");
        }
        
        if (record.getPayable() != null) {
            SET("payable = #{record.payable,jdbcType=INTEGER}");
        }
        
        if (record.getPayreal() != null) {
            SET("payreal = #{record.payreal,jdbcType=INTEGER}");
        }
        
        if (record.getDiscountrate() != null) {
            SET("discountrate = #{record.discountrate,jdbcType=INTEGER}");
        }
        
        if (record.getDiscount() != null) {
            SET("discount = #{record.discount,jdbcType=INTEGER}");
        }
        
        if (record.getOthercharge() != null) {
            SET("othercharge = #{record.othercharge,jdbcType=INTEGER}");
        }
        
        if (record.getComments() != null) {
            SET("comments = #{record.comments,jdbcType=VARCHAR}");
        }
        
        if (record.getOperatorid() != null) {
            SET("operatorid = #{record.operatorid,jdbcType=BIGINT}");
        }
        
        if (record.getPaytype() != null) {
            SET("paytype = #{record.paytype,jdbcType=INTEGER}");
        }
        
        if (record.getOrgid() != null) {
            SET("orgid = #{record.orgid,jdbcType=BIGINT}");
        }
        
        if (record.getDiscounttype() != null) {
            SET("discounttype = #{record.discounttype,jdbcType=INTEGER}");
        }
        
        if (record.getSinglecharge() != null) {
            SET("singlecharge = #{record.singlecharge,jdbcType=INTEGER}");
        }
        
        if (record.getClasscharge() != null) {
            SET("classcharge = #{record.classcharge,jdbcType=INTEGER}");
        }
        
        if (record.getOnrecord() != null) {
            SET("onrecord = #{record.onrecord,jdbcType=INTEGER}");
        }
        
        if (record.getGiverecords() != null) {
            SET("giverecords = #{record.giverecords,jdbcType=INTEGER}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("stu_contract");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("sid = #{record.sid,jdbcType=BIGINT}");
        SET("createtime = #{record.createtime,jdbcType=BIGINT}");
        SET("begintime = #{record.begintime,jdbcType=BIGINT}");
        SET("endtime = #{record.endtime,jdbcType=BIGINT}");
        SET("subid = #{record.subid,jdbcType=BIGINT}");
        SET("totalrecords = #{record.totalrecords,jdbcType=INTEGER}");
        SET("chargetime = #{record.chargetime,jdbcType=BIGINT}");
        SET("type = #{record.type,jdbcType=INTEGER}");
        SET("payable = #{record.payable,jdbcType=INTEGER}");
        SET("payreal = #{record.payreal,jdbcType=INTEGER}");
        SET("discountrate = #{record.discountrate,jdbcType=INTEGER}");
        SET("discount = #{record.discount,jdbcType=INTEGER}");
        SET("othercharge = #{record.othercharge,jdbcType=INTEGER}");
        SET("comments = #{record.comments,jdbcType=VARCHAR}");
        SET("operatorid = #{record.operatorid,jdbcType=BIGINT}");
        SET("paytype = #{record.paytype,jdbcType=INTEGER}");
        SET("orgid = #{record.orgid,jdbcType=BIGINT}");
        SET("discounttype = #{record.discounttype,jdbcType=INTEGER}");
        SET("singlecharge = #{record.singlecharge,jdbcType=INTEGER}");
        SET("classcharge = #{record.classcharge,jdbcType=INTEGER}");
        SET("onrecord = #{record.onrecord,jdbcType=INTEGER}");
        SET("giverecords = #{record.giverecords,jdbcType=INTEGER}");
        
        StuContractExample example = (StuContractExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(StuContract record) {
        BEGIN();
        UPDATE("stu_contract");
        
        if (record.getSid() != null) {
            SET("sid = #{sid,jdbcType=BIGINT}");
        }
        
        if (record.getCreatetime() != null) {
            SET("createtime = #{createtime,jdbcType=BIGINT}");
        }
        
        if (record.getBegintime() != null) {
            SET("begintime = #{begintime,jdbcType=BIGINT}");
        }
        
        if (record.getEndtime() != null) {
            SET("endtime = #{endtime,jdbcType=BIGINT}");
        }
        
        if (record.getSubid() != null) {
            SET("subid = #{subid,jdbcType=BIGINT}");
        }
        
        if (record.getTotalrecords() != null) {
            SET("totalrecords = #{totalrecords,jdbcType=INTEGER}");
        }
        
        if (record.getChargetime() != null) {
            SET("chargetime = #{chargetime,jdbcType=BIGINT}");
        }
        
        if (record.getType() != null) {
            SET("type = #{type,jdbcType=INTEGER}");
        }
        
        if (record.getPayable() != null) {
            SET("payable = #{payable,jdbcType=INTEGER}");
        }
        
        if (record.getPayreal() != null) {
            SET("payreal = #{payreal,jdbcType=INTEGER}");
        }
        
        if (record.getDiscountrate() != null) {
            SET("discountrate = #{discountrate,jdbcType=INTEGER}");
        }
        
        if (record.getDiscount() != null) {
            SET("discount = #{discount,jdbcType=INTEGER}");
        }
        
        if (record.getOthercharge() != null) {
            SET("othercharge = #{othercharge,jdbcType=INTEGER}");
        }
        
        if (record.getComments() != null) {
            SET("comments = #{comments,jdbcType=VARCHAR}");
        }
        
        if (record.getOperatorid() != null) {
            SET("operatorid = #{operatorid,jdbcType=BIGINT}");
        }
        
        if (record.getPaytype() != null) {
            SET("paytype = #{paytype,jdbcType=INTEGER}");
        }
        
        if (record.getOrgid() != null) {
            SET("orgid = #{orgid,jdbcType=BIGINT}");
        }
        
        if (record.getDiscounttype() != null) {
            SET("discounttype = #{discounttype,jdbcType=INTEGER}");
        }
        
        if (record.getSinglecharge() != null) {
            SET("singlecharge = #{singlecharge,jdbcType=INTEGER}");
        }
        
        if (record.getClasscharge() != null) {
            SET("classcharge = #{classcharge,jdbcType=INTEGER}");
        }
        
        if (record.getOnrecord() != null) {
            SET("onrecord = #{onrecord,jdbcType=INTEGER}");
        }
        
        if (record.getGiverecords() != null) {
            SET("giverecords = #{giverecords,jdbcType=INTEGER}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(StuContractExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}